import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {vehicleStatus, warrantyPolicyStatus, baseDataStatus} from '../Enum';
import {PAGE} from '../constants';
const baseUrl = '/afterSales/api/v1';

const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '整车索赔政策'
});
const brandDesc = formatMessage({
    id: 'api.message.brandDesc',
    defaultMessage: '查询品牌'
});
const DEFAULT_ORDER_QUERY = {
    status: [warrantyPolicyStatus.新建],
    pageIndex: PAGE.index,
    pageSize: 100,
    sortField: 'startTime',
    isDesc: true
};

// 获取品牌
export const getBrands = () => {
    const queryCondition = stringify({
        status: baseDataStatus.生效
    });
    return request(`${baseUrl}/brands?${queryCondition}`, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, brandDesc));
};


// 查询整车索赔政策
export const getWarrantyPolicy = data => {
    const queryString = stringify({
        ...DEFAULT_ORDER_QUERY,
        status: data.status
    });
    return request(`${baseUrl}/warrantyPolicies?${queryString}`, null)
        .then(handleFetchResultNotification(
            REQUEST_TYPE.query,
            formatMessage({
                id: 'api.message.getListDesc',
                defaultMessage: '查询{resourceName}'
            }, {resourceName})
        ));
};

// 查询整车索赔政策详情
export const getWarrantyPolicyDetail = id => request(`${baseUrl}/warrantyPolicies/${id}`, null)
    .then(handleFetchResultNotification(
        REQUEST_TYPE.initQuery,
        formatMessage({
            id: 'api.message.getDetailDesc',
            defaultMessage: '查询{resourceName}详情'
        }, {resourceName})
    ));

// 查询整车索赔政策条款
export const getWarrantyPolicyItem = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/warrantyPolicyItems?${queryString}`, null)
        .then(handleFetchResultNotification(
            REQUEST_TYPE.query,
            formatMessage({
                id: 'api.message.getItemListDesc',
                defaultMessage: '查询整车索赔政策条款'
            })
        ));
};

// 查询整车索赔政策条款详情
export const getWarrantyPolicyItemDetail = id => request(`${baseUrl}/warrantyPolicyItems/${id}`, null)
    .then(handleFetchResultNotification(
        REQUEST_TYPE.initQuery,
        formatMessage({
            id: 'api.message.getItemDetailDesc',
            defaultMessage: '查询整车索赔政策条款详情'
        })
    ));

// 新增
export const addWarrantyPolicy = data => request(`${baseUrl}/warrantyPolicies`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(
    REQUEST_TYPE.submit,
    formatMessage({
        id: 'api.message.addDesc',
        defaultMessage: '新增{resourceName}'
    }, {resourceName})
));

// 编辑
export const updateWarrantyPolicy = (id, data) => request(`${baseUrl}/warrantyPolicies/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(
    REQUEST_TYPE.submit,
    formatMessage({
        id: 'api.message.updateDesc',
        defaultMessage: '编辑{resourceName}'
    }, {resourceName})
));

// 生效
export const enableWarrantyPolicy = data => request(`${baseUrl}/warrantyPolicies/enable`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(
    REQUEST_TYPE.submit,
    formatMessage({
        id: 'api.message.enableDesc',
        defaultMessage: '生效{resourceName}'
    }, {resourceName})
));

// 作废
export const abandonWarrantyPolicy = data => request(`${baseUrl}/warrantyPolicies/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(
    REQUEST_TYPE.submit,
    formatMessage({
        id: 'api.message.abandonDesc',
        defaultMessage: '作废{resourceName}'
    }, {resourceName})
));

// 查询产品分类
export const getProductCategories = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/productCategories?${queryString}`, null)
        .then(handleFetchResultNotification(
            REQUEST_TYPE.query,
            formatMessage({
                id: 'api.message.getProductCategories',
                defaultMessage: '查询产品分类'
            })
        ));
};

// 查询车辆信息
export const getVehicles = queryCondition => {
    const queryString = stringify({
        ...queryCondition,
        status: [vehicleStatus.实销完成, vehicleStatus.经销商仓库]
    });
    return request(`${baseUrl}/vehicleSolds?${queryString}`, null)
        .then(handleFetchResultNotification(
            REQUEST_TYPE.query,
            formatMessage({
                id: 'api.message.getVehicles',
                defaultMessage: '查询车辆信息'
            })
        ));
};

// 查询配件索赔分类标准
export const getPartWarrantyCategories = queryCondition => {
    const queryString = stringify({
        ...queryCondition,
        status: baseDataStatus.生效,
    });
    return request(`${baseUrl}/partWarrantyCategories?${queryString}`, null)
        .then(handleFetchResultNotification(
            REQUEST_TYPE.query,
            formatMessage({
                id: 'api.message.getPartWarrantyCategories',
                defaultMessage: '查询配件索赔分类标准'
            })
        ));
};
//导入车辆模板
export const importTemp = () => request(`${baseUrl}/warrantyPolicies/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);
